
package restlet.android;

import java.util.ArrayList;

import restlet.android.BuildListActivity.GrocerWeListObserver;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cse210.grocerwe.client.SGroup;
import com.cse210.grocerwe.client.SItem;

public class ExpandableListAdapter extends BaseExpandableListAdapter {
	private static final int ITEM_DELETED = 1;
	private static final int ITEM_EDITED = 2;
    private BuildListActivity owner;
    private ArrayList<SGroup> groups;
    private ArrayList<ArrayList<SItem>> allItems;
    private GrocerWeListObserver listObserver;

    public ExpandableListAdapter(BuildListActivity context, ArrayList<SGroup> groups,
            ArrayList<ArrayList<SItem>> allItems) {
        this.owner = context;
        this.groups = groups;
        this.allItems = allItems;
    }
    public void addItemsToGroup(SItem[] items, SGroup group) {
    	int groupIndex = groups.indexOf(group);
    	
    	for(int i=0; i < items.length; i++){
    		allItems.get(groupIndex).add(items[i]);
    	}    	
    }
    
    public void addItemToGroup(SItem item, SGroup group) {
    	int groupIndex = groups.indexOf(group);
    	
    	allItems.get(groupIndex).add(item);
    }

    public void addGroup(SGroup group) {
    	groups.add(group);
    	allItems.add(new ArrayList<SItem>());
    }
    
    public void clearList(){
    	groups.clear();
    	allItems.clear();
    }
    
    @Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }
    
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return allItems.get(groupPosition).get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
    	SItem item = (SItem) getChild(groupPosition, childPosition);
    	ExpandableListPosTag holder;
    	
        if (convertView == null) {
            LayoutInflater mInflater = 
            	(LayoutInflater) owner.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.child_layout, null);
        }
        
        holder = new ExpandableListPosTag();
        holder.groupPosition = groupPosition;
        holder.childPosition = childPosition;
        holder.list = ExpandableListAdapter.this;
        
        TextView tvName = (TextView) convertView.findViewById(R.id.ItemName);
        TextView tvDesc = (TextView) convertView.findViewById(R.id.ItemDesc);
        tvName.setText(item.getName());
        tvDesc.setText(item.getDescription());
        
        LinearLayout itemInfo = (LinearLayout) convertView.findViewById(R.id.EachItemLinearLayout);
        
        ImageView iconSpace = (ImageView) convertView.findViewById(R.id.ItemOwnerPic);
        Drawable d;        
        if (item.getOwner().equals("grocerwe1")){
        	d = owner.getResources().getDrawable(R.drawable.user1);
        }
        else if (item.getOwner().equals("grocerwe2")){
        	d = owner.getResources().getDrawable(R.drawable.user2);
        }
        else if (item.getOwner().equals("grocerwe3")){
        	d = owner.getResources().getDrawable(R.drawable.user3);
        }
        else if (item.getOwner().equals("grocerwe4")){
        	d = owner.getResources().getDrawable(R.drawable.user4);
        }
        else {
        	d = owner.getResources().getDrawable(R.drawable.user5);
        }        
        iconSpace.setImageDrawable(d);        
        
        ImageButton deleteButton = (ImageButton) convertView.findViewById(R.id.ItemDeleteButton);
        
        if ( (item.getOwner().equals(owner.getUser()) && item.getStatus() == SItem.ST_NORMAL) ||
        		(item.getStatus() == SItem.ST_BOUGHT && item.getBuyer().equals(owner.getUser())) ){
        	deleteButton.setVisibility(View.VISIBLE);
	        deleteButton.setTag(holder);
	        deleteButton.setOnClickListener(new OnClickListener()
	        {            
	            @Override
	            public void onClick(View v)
	            {
	                ExpandableListPosTag holder = (ExpandableListPosTag) v.getTag();
	                int groupPosition = holder.groupPosition;
	                int childPosition = holder.childPosition;
	                SItem removedItem = (SItem) holder.list.getChild(groupPosition, childPosition);
	                allItems.get(groupPosition).remove(childPosition);
	                holder.list.notifyDataSetChanged(removedItem, ITEM_DELETED);
	            }
	        });
        }
        else {
        	deleteButton.setVisibility(View.INVISIBLE);
        }
        
        if (item.getOwner().equals(owner.getUser()) && item.getStatus() == SItem.ST_NORMAL){
	        itemInfo.setTag(holder);
	        itemInfo.setOnClickListener(new OnClickListener()
	        {            
	            @Override
	            public void onClick(View v)
	            {
	                ExpandableListPosTag holder = (ExpandableListPosTag) v.getTag();
	                int groupPosition = holder.groupPosition;
	                int childPosition = holder.childPosition;
	                SItem editedItem = (SItem) holder.list.getChild(groupPosition, childPosition);
	                holder.list.notifyDataSetChanged(editedItem, ITEM_EDITED);
	            }
	        });
        }
        else {
        	itemInfo.setClickable(false);
        }
        
        return convertView;
    }
    
    public void notifyDataSetChanged(SItem item, int how) {
    	switch(how){
    	case ITEM_DELETED:
    		listObserver.itemDeleted(item);
    		break;
    	case ITEM_EDITED:
    		listObserver.itemEdited(item);
    		break;
    	}
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return allItems.get(groupPosition).size();
    }
    
    public void registerDataSetObserver(GrocerWeListObserver observer){
    	this.listObserver = observer;    	
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        SGroup group = (SGroup) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = 
            	(LayoutInflater) owner.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.GroupTitleText);
        tv.setText(group.getName());
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }
    
    private class ExpandableListPosTag{
    	int groupPosition;
    	int childPosition;
    	ExpandableListAdapter list;
    }
}
